# 安包命令
# pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
# pip install pyquery -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
# pip install pandas -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
import random
import time
import requests
from pyquery import PyQuery as pq
import pandas as pd

# 请求30页，最多有70多页，根据需要写，如果需要全部写10000即可
pagenum = 30
# 输出文件的名字
file_csv = "res.csv"

value_list = []


# 获取信息
def get_info():
    global key_lin
    global key_list
    global value_list
    # 清空上次的内容
    with open(file_csv, 'w', encoding='utf-8') as f:
        f.write("")
    for i in range(pagenum):
        # doc = pq(requests.get("https://eth.bitaps.com/blocks?page={}".format(i), headers=headers).content.decode())
        print("第 {} 页开始".format(i + 1))
        headers = {
            'authority': 'eth.bitaps.com',
            'accept': '*/*',
            'accept-language': 'zh-CN,zh;q=0.9',
            'content-length': '0',
            'cookie': 'hide_dashboard=0',
            'origin': 'https://eth.bitaps.com',
            'referer': 'https://eth.bitaps.com/blocks?page=23',
            'sec-ch-ua': '^\\^',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '^\\^Windows^\\^',
            'sec-fetch-dest': 'empty',
            'sec-fetch-mode': 'cors',
            'sec-fetch-site': 'same-origin',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36',
            'x-requested-with': 'XMLHttpRequest',
        }
        params = (('page', i + 1),)
        response = (
            requests.post('https://eth.bitaps.com/blocks/list', headers=headers, params=params)).content.decode()
        # print(response)
        # print("-" * 50)
        doc = pq(response)
        # print(doc)

        # 遍历全部的table
        for item in doc(".tml-table-wrap").items():
            key_list = []
            for tr in item("tr").items():
                key = tr("td:nth-child(1)").text()
                value = tr("td").text().strip(key).strip()
                key_list.append(key)
                value_list.append(value)
                key_lin = len(key_list)
        # 整理格式
        print("key_list:{}".format(key_list))
        key_list2 = key_list[:(key_lin + 1)]
        # print("key_list2:{}".format(key_list2))
        # print("value_list:{}".format(value_list))
        res_all = []
        for j in range(len(value_list) // key_lin):
            value_list_1 = value_list[(j * key_lin):((j + 1) * key_lin)]
            print("value_list_1:{}".format(value_list_1))
            dataframe = pd.DataFrame({'key_list': key_list, 'value': value_list_1})
            dataframe.to_csv(file_csv, mode='a', index=False, sep=',')

        time.sleep(random.randint(1, 5))  # 请求太快不行
        print("第 {} 页结束".format(i + 1))

def main():
    get_info()

if __name__ == '__main__':
    main()
